//
//  MainViewController.swift
//  flyingdogWB
//
//  Created by SLASH on 16/8/2.
//  Copyright © 2016年 SLASH. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置当前控制器tabbar颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        
        if let jsonPath = path
        {
            let jsonData = NSData(contentsOfFile: jsonPath)
            
            do
            {
                //有可能发生异常的代码
                //try :发生异常时会跳到catch中
                //try! :发生异常时程序直接崩溃
                //序列化json数据
                let dictArray = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                print(dictArray)
                //遍历数组必须明确数据的类型
                for dict in dictArray as! [[String: String]]
                {
                    let imageName = dict["imageName"]
                    
                    let title = dict["title"]
                    
                    let vcName = dict["vcName"]
                    
                    addChildViewController(vcName!, title: title!, imageName: imageName!)
                }
            }
            catch
            {
                //发生异常之后执行的代码
                print(error)
                
                //异常后从本地加载控制器
                addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
                
                addChildViewController("MessgaeTableViewController", title: "消息", imageName: "tabbar_message_center")
                
                addChildViewController("SquareTableViewController", title: "发现", imageName: "tabbar_discover")
                
                addChildViewController("PersonalTableViewController", title: "我", imageName: "tabbar_profile")
            }
        }
    }
    
    /**
     配置并添加自控制器
     
     - parameter childController: 子控制器
     - parameter title:           标题
     - parameter imageName:       tabbar图标
     */
//    private func addChildViewController(childController: UIViewController, title: String, imageName: String)
//    {
    private func addChildViewController(childControllerName: String, title: String, imageName: String)
    {

        //命名空间默认是项目名称，但是命名空间可以修改（修改productName）
        //动态获取命名空间
        let productName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        //将字符串转化为类
        let anyClass:AnyClass? = NSClassFromString(productName + "." + childControllerName)
        
        //将anyClass转换成指定类型
        let vcClass = anyClass as! UIViewController.Type
        
        //通过class创建对象
        let childController = vcClass.init()
        
        //设置tabbar对应数据
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //        home.tabBarItem.title = title
        
        //设置导航栏标题
        //        home.navigationItem.title = title
        childController.title = title
        
        //包装导航控制器
        let navi = UINavigationController()
        navi.addChildViewController(childController)
        
        //将导航控制器添加到标签栏控制器
        addChildViewController(navi)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
